//
//  startView.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/22.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "startView.h"

@implementation startView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        CGFloat mrg = 2;
        for (int i = 0; i < 5; i++) {
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
            UIImageView *star = [UIImageView new];
            [star addGestureRecognizer:tap]; star.tag = i;
            star.userInteractionEnabled = YES;
            star.image = [UIImage imageNamed:@"greaStars"];
            star.frame = CGRectMake(i*(15+mrg), 0, 15, 15);
            [self addSubview:star];
        }
    }
    return self;
}

- (void)setNumber:(NSString *)number {
    //float f = 1.6; int a; a = floor(f); NSLog("a = %d",a);
    float a = number.floatValue;
    int b = floor(a);//整个星
    float c = a - b;//半个星
    NSArray *subViews = [self subviews];
    for (UIImageView *V in subViews) {
        if (b > 0) {
            V.image = [UIImage imageNamed:@"yellowStars"];
            b--;
        }else if (c > 0) {
            V.image = [UIImage imageNamed:@"halfStars"];
            c = 0.0;
        }
    }
}
- (void)tapClick:(UITapGestureRecognizer *)tap {
    UIImageView *tapView = (UIImageView *)tap.view;
    NSInteger num = tapView.tag + 1;
    self.starNumBlock(num);
    for (UIImageView *view in self.subviews) {
        if (view.tag <= tapView.tag) {
            view.image = [UIImage imageNamed:@"yellowStars"];
        }else {
            view.image = [UIImage imageNamed:@"greaStars"];
        }
    }
}
@end
